﻿
using UnityEngine;

public class Paradox : MonoBehaviour
{
    private Transform transform_mainCamera;

    [SerializeField]
    [Range(0.001f, 1000f)]
    private float depth;        //深度，挂载此脚本的物体在镜头中的移动速度为一般物体的1/Depth，未挂载此脚本相当于depth为1
    private Vector3 cameraPosition;
    private Vector3 offset;     //上一固定帧内相机的位移

    private void Start()
    {
        transform_mainCamera = Camera.main.transform;
        SpriteRenderer sp = GetComponent<SpriteRenderer>();
        if (sp != null)
            sp.sortingOrder = (int)-depth;
        cameraPosition = transform_mainCamera.position;
    }

    private void Update()
    {
        offset = transform_mainCamera.position - cameraPosition;
        cameraPosition = transform_mainCamera.position;
        //先跟随相机（保持在屏幕中位置的较上一帧不变），再反方向移动一段距离
        transform.position += (1 - 1 / depth) * offset;
    }
}

//暂时弃用
public class Random_Paradox
{
    public static bool[] Nums = new bool[40];
    public static int RandomDepth()
    {
        int num = Random.Range(10, 40);
        for (; Nums[num];)
        {
            num = Random.Range(10, 40);
        }
        Nums[num] = true;
        return num / 2;
    }
    public static float RandomAlphaRandge() => Random.Range(0.4f, 0.6f);
    public static float RandomScaleRandge() => Random.Range(0.6f, 0.8f);

    public static bool[] Blocks = new bool[25];      //交叉点是否被使用过
    public static Vector3 RandomPos()
    {
        int num = Random.Range(0, 25);
        for (; Blocks[num];)
        {
            num = Random.Range(0, 25);
        }
        Blocks[num] = true;
        Vector2 pos = new Vector2(num / 5 * 10f - 5f, num % 5 * 10f - 25f) + Random.Range(0f, 3f) * (Random.Range(0f, 360f)).ToDirection();
        return pos;
    }
    public static void Reset()
    {
        for (int i = 0; i < 40; i++)
        {
            Nums[i] = false;
        }
        for (int i = 0; i < 25; i++)
        {
            Blocks[i] = false;
        }
    }
}